import {
  exportUrl,
  getTypeCodeList,
  getStatusList,
  downloadTemplateUrl,
} from "@/api/materialStation/mesMaterialStationModel";
import {
  getLineCodeList,
  getWorkCellCodeList,
} from "@/api/employee/mesUserExtend";
import { getUnitDictList } from "@/api/common";
import { exportExcel, validateGZInteger } from "@/utils/judge";
// 列表头按钮配置
export const operateList = [
  {
    name: $i18n.t("common.export"),
    type: "primary",
    icon: "el-icon-download",
    handleName: "export", //点击时返回的标识
    url: exportUrl,
    //auth: "auth:xx:export", //权限字段
  },
  {
    name: $i18n.t("common.add"),
    type: "primary",
    icon: "el-icon-circle-plus-outline",
    handleName: "add", // 点击时返回的标识
    // auth: "auth:user:add", //权限字段
  },
  {
    name: $i18n.t("common.delete"),
    type: "danger",
    icon: "el-icon-delete",
    handleName: "delete",
    isMultiple: true, // isSingle 表示必须选择一项//isMultiple: true, // 表示必须多选
    // auth: "auth:user:delete", //权限字段
  },
  {
    name: $i18n.t("common.import"),
    type: "primary",
    icon: "el-icon-upload2",
    handleName: "import", //点击时返回的标识
  },
];
// 搜索条件配置
export const searchConfig = {
  inline: true,
  column: 3,
  formDesc: {
    stationCode: {
      type: "input",
      label: $i18n.t("mesMaterialStationModel.stationCode"),
    },
    stationName: {
      type: "input",
      label: $i18n.t("mesMaterialStationModel.stationName"),
    },
    equipmentCode: {
      type: "input",
      label: $i18n.t("mesMaterialStationModel.equipmentCode"),
    },
    equipmentName: {
      type: "input",
      label: $i18n.t("mesMaterialStationModel.equipmentName"),
    },
    typeCode: {
      type: "select",
      label: $i18n.t("mesMaterialStationModel.typeCode"),
      prop: {
        value: "typeCode",
        text: "typeName",
      },
      options: async (data) => {
        const list = await getTypeCodeList();
        return list.data.result;
      },
    },
    status: {
      type: "select",
      label: $i18n.t("mesMaterialStationModel.status"),
      prop: {
        value: "value",
        text: "description",
      },
      options: async (data) => {
        const list = await getStatusList();
        return list.data.result;
      },
    },
    prodCode: {
      type: "input",
      label: $i18n.t("mesMaterialStationModel.prodCode"),
    },
    prodName: {
      type: "input",
      label: $i18n.t("mesMaterialStationModel.prodName"),
    },
    materialCode: {
      type: "input",
      label: $i18n.t("mesMaterialStationModel.materialCode"),
    },
    materialName: {
      type: "input",
      label: $i18n.t("mesMaterialStationModel.materialName"),
    },
    updateTime: {
      type: "daterange",
      label: $i18n.t("mesMaterialStationModel.timeArea"),
      attrs: {
        valueFormat: "yyyy-MM-dd",
        searchArrKey: ["searchDateMin", "searchDateMax"],
      },
    },
  },
};
// 表格的行内操作
export const rowConfig = [
  {
    name: $i18n.t("common.edit"),
    type: "primary",
    class: "row-btn-primary",
    handleName: "edit", // 点击时返回的标识
    // auth: "auth:user:edit", //权限字段
  },
  {
    name: $i18n.t("common.details"),
    type: "info",
    class: "row-btn-info",
    handleName: "info", // 点击时返回的标识
  },
  {
    name: $i18n.t("common.delete"),
    type: "danger",
    class: "row-btn-danger",
    handleName: "delete", //点击时返回的标识
  },
];

// 新增/删除表单配置
export const formDescDialogMixin = {
  data() {
    const self = this;
    return {
      formDescDialog: {
        span: 24,
        // 表单是否可编辑
        disabled: true,
        // 是否显示提交按钮
        isShowSubmitBtn: true,
        formDesc: {
          workshopCode: {
            type: "select",
            label: $i18n.t("mesMaterialStationModel.workshopName"),
            layout: 12,
            required: true,
            attrs: {
              filterable: true,
            },
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              let params = {
                modelingCode: self.$store.getters.factoryCode,
                level: 4,
              };
              let list = await getLineCodeList(params);
              self.submitFarmList = list.data.result;
              return list.data.result;
            },
            on: {
              change() {
                self.formDataDialog.equipmentCode = "";
                self.formDataDialog.equipmentName = "";
                self.formDataDialog.lineCode = "";
                self.formDataDialog.lineName = "";
                self.formDataDialog.workCellCode = "";
                self.formDataDialog.workCellName = "";
                // self.formDataDialog.workCellCode3 = "";
                self.formDataDialog.lineCode2 = "";

                self.$set(self.formDataDialog, "workCellCode3", "");
              },
            },
          },
          farmCode2: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.workshopCode"),
            layout: 12,
            required: true,
            disabled: true,
            vif(data) {
              if (!!data.workshopCode) {
                debugger;
                let item = self.submitFarmList.find(
                  (val) => val.modelingCode == data.workshopCode
                );
                if (!!item) {
                  data.farmName = item.modelingName;
                  data.farmCode2 = item.modelingCode;
                  data.workshopName = item.modelingName;
                }
              }
              return true;
            },
          },
          lineCode: {
            type: "select",
            label: $i18n.t("mesMaterialStationModel.lineName"),
            layout: 12,
            optionsLinkageFields: ["workshopCode"],
            // required: true,
            attrs: {
              filterable: true,
            },
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              self.submitLineList = [];
              if (!data.workshopCode) return [];
              let res = await getWorkCellCodeList({
                modelingCode: data.workshopCode,
              });
              let { result } = res.data;
              self.submitLineList = result || [];
              return self.submitLineList;
            },
            on: {
              change(e) {
                self.formDataDialog.equipmentCode = "";
                self.formDataDialog.equipmentName = "";
                self.formDataDialog.workCellCode = "";
                self.formDataDialog.workCellName = "";
                self.formDataDialog.workCellCode3 = "";

                if (!e) {
                  self.$set(self.formDataDialog, "lineCode2", "");
                }
              },
            },
          },
          lineCode2: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.lineCode"),
            layout: 12,
            // required: true,
            disabled: true,
            vif(data) {
              if (!!data.lineCode) {
                let item = self.submitLineList.find(
                  (val) => val.modelingCode == data.lineCode
                );
                if (!!item) {
                  self.$set(data, "lineCode2", item.modelingCode);
                  data.lineCode2 = item.modelingCode;
                  data.lineName = item.modelingName;
                }
              }
              return true;
            },
          },
          workCellCode: {
            type: "select",
            label: $i18n.t("mesMaterialStationModel.workCellName"),
            layout: 12,
            optionsLinkageFields: ["lineCode"],
            // required: true,
            attrs: {
              filterable: true,
            },
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              self.submitCellList = [];
              if (!data.lineCode) return [];
              let res = await getWorkCellCodeList({
                modelingCode: data.lineCode,
              });
              let { result } = res.data;
              self.submitCellList = result || [];
              return self.submitCellList;
            },
            on: {
              change(e) {
                self.formDataDialog.equipmentCode = "";
                self.formDataDialog.equipmentName = "";
                // self.formDataDialog.workCellCode2 = "";

                if (!e) {
                  self.$set(self.formDataDialog, "workCellCode3", "");
                }
              },
            },
          },
          workCellCode3: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.workCellCode"),
            layout: 12,
            // required: true,
            disabled: true,
            vif(data) {
              if (!!data.workCellCode) {
                let item = self.submitCellList.find(
                  (val) => val.modelingCode == data.workCellCode
                );
                if (!!item) {
                  // self.$set(
                  //   self.formDataDialog,
                  //   "workCellCode2",
                  //   item.modelingCode
                  // );
                  self.$set(data, "workCellCode3", item.modelingCode);

                  data.workCellName = item.modelingName;
                  console.log(999, data);
                }
              }
              return true;
            },
          },
          stationCode: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.stationCode"),
            layout: 12,
            required: true,
            disabled: true,
          },
          stationName: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.stationName"),
            layout: 12,
            required: true,
            disabled: true,
          },
          equipmentCode: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.equipmentCode"),
            layout: 12,
            required: false,
            disabled: true,
          },
          equipmentName: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.equipmentName"),
            layout: 12,
            required: false,
            disabled: true,
          },
          prodCode: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.prodCode"),
            layout: 12,
            required: true,
            disabled: true,
          },
          prodName: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.prodName"),
            layout: 12,
            required: true,
            disabled: true,
          },
          materialCode: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.materialCode"),
            layout: 12,
            required: true,
            disabled: true,
          },
          materialName: {
            type: "input",
            label: $i18n.t("mesMaterialStationModel.materialName"),
            layout: 12,
            required: true,
            disabled: true,
          },
          volume: {
            type: "number",
            label: $i18n.t("mesMaterialStationModel.volume"),
            layout: 12,
            required: true,
            attrs: {
              min: 0,
              max: 999999999999,
            },
          },
          unitCode: {
            type: "select",
            label: $i18n.t("mesMaterialStationModel.unit"),
            layout: 12,
            required: true,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getUnitDictList();
              return list.data.result;
            },
            on: {
              change: (val) => {
                self.handelSelectEqu(val);
              },
            },
          },
          safetyStock: {
            type: "number",
            label: $i18n.t("mesMaterialStationModel.safetyStock"),
            layout: 12,
            required: true,
            attrs: {
              min: 0,
              max: 999999999999,
            },
          },
          unitConsumption: {
            type: "number",
            label: $i18n.t("mesMaterialStationModel.unitConsumption"),
            layout: 12,
            required: true,
            attrs: {
              min: 0,
              max: 999999999999,
            },
            // rules: [
            //   { validator: validateGZInteger, trigger: ["change", "blur"] },
            // ],
          },
          status: {
            type: "switch",
            label: $i18n.t("mesMaterialStationModel.enable"),
            layout: 12,
            required: false,
            // 只能是0 和 1
            valueFormatter(labels) {
              return labels == "1" ? "1" : "0";
            },
            displayFormatter(labels) {
              return labels == "1" ? true : false;
            },
          },
        },
      },
    };
  },
  methods: {
    handelSelectEqu(val) {
      let description = (
        this.getDictList.find((i) => i.value == val) || { description: "" }
      ).description;
      this.$set(this.formDataDialog, "unit", description);
    },
  },
};

//导入数据
export const importFormDescDialog = {
  span: 24,
  disabled: false,
  isShowSubmitBtn: true,
  formDesc: {
    model: {
      type: "button",
      label: "",
      layout: 24,
      required: false,
      title: $i18n.t("common.downloadTemplate"),
      style: {
        color: "#1867c0",
        fontSize: "14px",
      },
      on: {
        click: () => {
          window.open(downloadTemplateUrl);
        },
      },
    },
    file: {
      type: "input",
      label: $i18n.t("mesMaterialStationModel.file"),
      layout: 24,
      // required: true,
    },
  },
};
